package com.videomeetting.legend.videomeetting.activity.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.videomeetting.legend.videomeetting.activity.base.BaseApplication;
import com.videomeetting.legend.videomeetting.activity.bean.VideoBean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by legend on 18-7-10.
 */

public class MyUtils {


    public static boolean hasWifiConnection(Context context)
    {
        final ConnectivityManager connectivityManager= (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        //是否有网络并且已经连接
        return (networkInfo!=null&& networkInfo.isConnectedOrConnecting());
    }

    /**
     *
     * @param context
     * @param uri
     * @param code 0 1
     * @return
     */
    public static List<VideoBean> queryLocalVideo(Context context, Uri uri, int code) {
        List<VideoBean> videoBeans = new ArrayList<>();
        String[] projection = new String[]{MediaStore.Video.Media.DATA, MediaStore.Video.Media
                .DURATION};
        ContentResolver contentResolver = context.getContentResolver();//Uri.parse("file://" + Environment.getDataDirectory())
        Cursor cursor = contentResolver.query(uri, null, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                VideoBean bean = new VideoBean();
//                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
//                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));// 视频的id
                // 视频名称
                bean.setName(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)));
//                String resolution = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.RESOLUTION)); //分辨率
                // 大小 byte
                long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
                String sizeMB = String.valueOf((int) (size / (1024 * 1024)));
                bean.setLenght(sizeMB);
                // 时长 ms
                long duration = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));// 时长
                String time = String.valueOf((int) duration / (1000 * 60));
                bean.setTime(time);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                String path = cursor.getString(column_index);
                bean.setFilePath(path);
                if (path.contains("usbport")&&code==1){
                    Log.i(BaseApplication.TAG, "queryLocalVideo: U"+bean.getFilePath());
                    videoBeans.add(bean);
                }
                if (code==0&&path.contains("emulated")){//emulated
                    Log.i(BaseApplication.TAG, "queryLocalVideo: Local"+bean.getFilePath());
                    videoBeans.add(bean);
                }
//                long date = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_MODIFIED));//修改时间
            }
            cursor.close();
        }

        return videoBeans;
    }
}
